package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;
import com.itheima.edu.info.manager.util.DateUtil;

import java.io.FileNotFoundException;
import java.util.Scanner;

public class StudentController {
    //提升成员变量
    private Scanner sc = new Scanner(System.in);
    private StudentService studentService = new StudentService();
    //用来开启学生管理系统并展示学生管理系统菜单
    public void start() throws FileNotFoundException {
        while (true) {
            System.out.println("-----欢迎来到学生管理系统-----");
            System.out.println("请输入您的选择:1.添加学生2.删除学生3.修改学生4.查询学生5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStundetById();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    return;
                default:
                    System.out.println("您的输入有误,请重新输入!");
                    break;
            }
        }
    }
    //修改成功
    public void updateStudent() {
        StudentService studentService = new StudentService();
        //查数组有无数据
        Student[] students = studentService.findAllStudent();
        if (students == null) {
            System.out.println("暂无数据,重新输入");
            return;
        }
        String updateId;
        while (true) {
            System.out.println("请输入要修改的学生id:");
            updateId = sc.next();
            boolean exists = studentService.isExists(updateId);
            if (!exists) {
                System.out.println("不存在,请重新输入");
            } else {
                break;
            }
        }
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生生日:");
        String birthday = sc.next();
        String age = DateUtil.getAge(birthday);
        //封装对象
        Student stu = new Student(updateId, name, age, birthday);
        studentService.updateStundet(stu);
        System.out.println("修改成功");
    }

    //添加学生方法
    public void addStudent() throws FileNotFoundException {
        String id;
        //键入信息
        while (true) {
            System.out.println("请输入学生ID:");
            id = sc.next();
            //判断学号是否存在
            StudentService studentService1 = new StudentService();
            boolean flag = studentService1.isExists(id);
            if (flag) {
                System.out.println("学号已被占用,重新输入:");
            } else {
                break;
            }
        }
        System.out.println("请输入学生姓名:");
        String name = sc.next();

        System.out.println("请输入学生生日:");
        String birthday = sc.next();
        String age = DateUtil.getAge(birthday);
        //封装对象
        Student stu = new Student(id, name, age, birthday);
        StudentService studentService = new StudentService();
        boolean flag = studentService.addStudent(stu);
        //根据业务员返回值判断给出相应的输出结果
        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    //查看学生方法
    public void findAllStudent() {
        StudentService studentService = new StudentService();
        Student[] students = studentService.findAllStudent();
        if (students == null) {
            System.out.println("暂无数据");
            return;
        } else {
            System.out.println("学号\t姓名\t年龄\t\t生日");
            for (int i = 0; i < students.length; i++) {
                Student student = students[i];
                if (student != null) {
                    System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t\t" + student.getBirthday());
                }
            }
        }
    }

    //删除学生方法
    public void deleteStundetById() {
        StudentService studentService = new StudentService();
        //查数组有无数据
        Student[] students = studentService.findAllStudent();
        /*
        boolean empty = true;
        for (int i = 0; i < students.length; i++) {
            if(students[i]!=null){
                empty = false;
            }
        }
        if (empty){
            System.out.println("暂无数据,重新输入");
        }
        */
        if (students == null) {
            System.out.println("暂无数据,重新输入");
            return;
        }
        String delId;
        while (true) {
            System.out.println("请输入要删除的学生id:");
            delId = sc.next();
            boolean exists = studentService.isExists(delId);
            if (!exists) {
                System.out.println("不存在,请重新输入");
            } else {
                break;
            }
        }
        studentService.deleteStudentById(delId);
        System.out.println("删除成功");
    }

    //封装方法录入id
    public String inputStundetId(String id,boolean flag){
        while (true) {
            System.out.println("请输入要删除的学生id:");
            id = sc.next();
            boolean exists = studentService.isExists(id);
            if (exists == flag) {
                System.out.println("不存在,请重新输入");
            } else {
                return id;
            }
        }
    }
}
